﻿/////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2009 Eric Bowen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
// associated documentation files (the "Software"), to deal in the Software without restriction, including 
// without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
// copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the 
// following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial 
// portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/////////////////////////////////////////////////////////////////////////////////////////////////////
// Author's blog: http://blog.scrappydog.com
// Updated source code available:  http://scrappydb.codeplex.com
/////////////////////////////////////////////////////////////////////////////////////////////////////

using System;
using System.Globalization;

namespace ScrappyDB.Utilities
{
    public static class DbDateTime
    {
        private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
       
        /// <summary>
        /// Converts a DateTime to an Iso8601 string
        /// </summary>
        /// <param name="dt"></param>
        /// <returns>Note: Milliseconds are dropped on save.</returns>
        public static String ConvertDateTimeToIso8601(DateTime dt)
        {
            var result = dt.ToUniversalTime().ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'");
            return result;
        }

        public static DateTime ConvertIso8601ToDateTime(String iso)
        {
            DateTime result;
            if (iso.Contains("T") & iso.Contains("Z"))
            {
                 result = DateTime.ParseExact(iso, "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'", CultureInfo.InvariantCulture);
            }
            else
            {
                //this is a not the default format we expected, but it still could be a valid "less standard" value, so lets give it a try...
                result = DateTime.ParseExact(iso, "yyyy'-'MM'-'dd' 'HH':'mm':'ss", CultureInfo.InvariantCulture);
            }
            
            result = DateTime.SpecifyKind(result, DateTimeKind.Utc);

            return result;
        }

        /// <summary>
        /// Converts a .NET DateTime to a standard Unix TimeStamp
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static double DateTimeToUnixTimeStamp(DateTime dateTime)
        {
            TimeSpan ts = (dateTime.ToUniversalTime() - UnixEpoch);
            double unixTime = ts.TotalSeconds;
            return unixTime;
        }

        public static DateTime UnixTimeStampToDateTime(double ts)
        {
            DateTime newDateTime = UnixEpoch.AddSeconds(ts);
            return newDateTime;
        }

    }
}